<?php
/**
* Change logs
* Feb 9, 2011 - trungdt - Add demand_split_content
* Mar 11, 2011 - trungdt - Add pray count (LIKE BUTTON)
*/
class demandmodule {
    function name() { return "Demand Management module"; }
    function description() { return "Demands management, for PrayOnline"; }
    function author() { return 'ABSoft'; }

    function hasContent() { return true; }
    function hasSources() { return true; }
    function hasViews()   { return true; }

    function supportsWorkflow() { return false; }

    function permissions($internal = '') {
        return array(
        "approve_demand"=>"Duyệt lời cầu nguyện",
        "administrate"=>"Admin",        
        "manage"=>"Quản lý/Xóa tin",
        );
    }

    function deleteIn($loc) {
        // Do nothing, no content
    }

    function copyContent($from_loc,$to_loc) {
        // Do nothing, no content
    }

    function getLocationHierarchy($loc) {
        // because this module does not belong to any page, we fake source to get permission
        $loc->src="";
        $loc->int='';
        return array($loc,exponent_core_makelocation($loc->mod,$loc->src));
    }

    function show($view,$loc = null, $title = '') {
        $template = new template('praymodule',$view,$loc);

        $template->output($view);
    }

    function ab_spiderItem($item,$type="demand")
    {
        if (!defined('SYS_SEARCH')) require_once(BASE.'subsystems/search.php');    
        if (isset($item->status) && ($item->status != 2)) 
        {
            return;
        }
        global $db;
        $db->delete('search',"ref_module='demandmodule' AND ref_type='{$type}' AND original_id=" . $item->id);
        $search = null;
        $search->category = "PrayItem";
        $search->ref_module = 'demandmodule';
        $search->ref_type = "{$type}";
        $search->original_id = $item->id;
        $search->title = " ".$item->name ." ";
        $params=array(
        "action"=>"{$type}_detail",
        "module"=>"demandmodule",
        "id"=>($item->id."-"._to_vietnamese_with_nomark($item->name))
        );
        $search->view_link = exponent_core_makeLink($params);
        $i=strpos($search->view_link,BASE_URL);
        if ($i===0)
            $search->view_link=substr($search->view_link,strlen(BASE_URL)+1);
        $search->body = $item->content;
        $search->dont_trim=0;
        $search->keywords=" ".exponent_search_removeHTML($item->name." ".$item->content)." ";
        $search->location_data = "";
        $db->insertObject($search,'search');
    }

    function searchName(){
        $i8 = exponent_lang_loadFile('modules/demandmodule/demand.php');
        return $i8['demand_by_day'].'-'.$i8['witness_by_day'];

    }

    /**
    * Spider Content, for Rebuild search
    * 
    */
    function spiderContent($item = null) {
        global $db;
        if ($item==null)
        {
            if (!defined('SYS_SEARCH')) require_once(BASE.'subsystems/search.php'); 

            $db->delete('search',"ref_module='demandmodule' AND ref_type='demand'");
            $db->delete('search',"ref_module='demandmodule' AND ref_type='witness'");
            foreach ($db->selectObjects('demands',"status = 2") as $item) {
                demandmodule::ab_spiderItem($item,"demand");
            }
            foreach ($db->selectObjects('witness',"status = 2") as $item) {
                demandmodule::ab_spiderItem($item,"witness");
            }
        }
        return true;
    }

    /**
    * Split content for demand module
    * 
    * @param mixed $content
    * @param mixed $content_hidden
    * @author: trungdt
    */
    static function demand_split_content(&$content, &$content_hidden)
    {
        $content_hidden="";
        $temp_space=strpos($content,".");
        if (!($temp_space === false))
        { 
            if ($temp_space < strlen($content))
            {
                $temp_space++;
            }
            $content_hidden=trim(substr($content,$temp_space));
            $content=substr($content,0, $temp_space);
            $content_hidden=str_replace("\n","<br>",$content_hidden);
        }
        $content=str_replace("\n","<br>",$content);
    }

    /**
    * Prepare to show demand and witness
    * 
    * @param object $data data after sql by object
    * @param mixed $page current page
    * @param mixed $pages how many pages
    * 
    * @return template
    */
    static function demand_witness_show(&$data, $lang, $lang2, $show_notapprove=false){
        global $user, $db;
        
        if (!isset($user->time_belt))
        {
            $user->time_belt=0;
        }
        
        // make data pretty
        // we make this output because we must find all witness of each demand
        foreach ($data as $key=>$value)
        {
            // Mar 11, 2011 - trungdt - Support LIKE Button - begin
            if ($value->type == 1 ) // demand
            {
                // is this user clicked LIKE button before?
                if (exponent_users_isLoggedIn() && ($user->id != $value->user_id))
                {
                    $value->demand_pray_count_notlogged=1;
                    $temp=$db->selectObject("demand_praydetail","user_id = {$user->id} AND demand_id = {$value->id}");
                    if ($temp != null)
                    {
                        $temp->add_time=$temp->add_time-(7+$user->time_belt)*3600; // From GMT +7 to local time
                        $value->pray_count_userdate=date('d/m/Y h:i:s',$temp->add_time) ." (GMT {$user->time_belt})";
                        $value->demand_pray_count_notlogged=2; // prayed, dont allow to click again
                    }
                }
                elseif (!exponent_users_isLoggedIn())
                {
                    // this user is not logged, tell template that alert Visitor must login to first
                    $value->demand_pray_count_notlogged=0;
                }
                else
                {
                    $value->demand_pray_count_notlogged=4;
                }
            }
            // Mar 11, 2011 - trungdt - Support LIKE Button - end
            $value->post_date=$value->post_date-(7+$user->time_belt)*3600; // From GMT +7 to local time
            
            $value->small_time=date('d/m/Y \<\b\r\> h:i:s',$value->post_date) ." (GMT {$user->time_belt})";
            $value->post_date = date('d/m/Y h:i:s A',$value->post_date) ." (GMT {$user->time_belt})";
            $value->seo_title=($value->id."-"._to_vietnamese_with_nomark($value->name));
            $value->subject_name_safe=rawurlencode($value->subject_name);
            // for approve status
            if ($value->status == 0)
            {
                $value->demand_tip = $lang['status_waiting']; // waiting 
            }
            else
            {
                // when approved, approve date is also publish date
                 $value->publish_date=$value->publish_date-(7+$user->time_belt)*3600; // From GMT +7 to local time
                 
                $approve_date=date($lang['status_approve_dateformat'],$value->publish_date);
                $approve_time=date($lang['status_approve_timeformat'],$value->publish_date)." (GMT {$user->time_belt})";
                $approve_username=$value->unapprove_username;
                if ($value->status == 1)
                {
                    if (!$show_notapprove)
                        $value->demand_tip=$lang['status_unapprove'];
                    else
                        $value->demand_tip=$lang['status_unapprove_admin'];
                }
                else
                {
                    if (!$show_notapprove)
                        $value->demand_tip=$lang['status_approved'];
                    else
                        $value->demand_tip=$lang['status_approved_admin'];
                }
                $value->demand_tip = str_replace("%u",$approve_username,$value->demand_tip);
                $value->demand_tip = str_replace("%d",$approve_date,$value->demand_tip);
                $value->demand_tip = str_replace("%t",$approve_time,$value->demand_tip);
            }
            // end of approve status
            // split content into 2 part, for showing
            demandmodule::demand_split_content($value->content, $value->content_hidden);

            if ($value->type == 1)
            {
                // for witness - begining
                $wit_where="demand_id = {$value->id}";
                if ($show_notapprove == false)
                {
                    $wit_where.=" AND (status = 2)";
                }
                $temp=$db->selectObjectsBySql("
                    SELECT * FROM {$db->prefix}witness 
                    WHERE {$wit_where}
                    ORDER BY status, post_date DESC, publish_date DESC
                ");
                foreach ($temp as $k=>$witness)
                {
                    $witness->important_level=7;
                    $witness->post_date=$witness->post_date-(7+$user->time_belt)*3600; // From GMT +7 to local time
                    $witness->small_time=date('d/m/Y \<\b\r\> h:i:s',$witness->post_date)." (GMT {$user->time_belt})";

                    if ($witness->status == 0)
                    {
                        $witness->demand_tip = $lang['status_waiting']; // waiting 
                    }
                    else
                    {
                        // when approved, approve date is also publish date
                        $wapprove_date=date($lang['status_approve_dateformat'],$witness->publish_date);
                        $wapprove_time=date($lang['status_approve_timeformat'],$witness->publish_date);
                        $wapprove_username=$witness->unapprove_username;

                        if ($witness->status == 1)
                        {
                            if (!$show_notapprove)
                                $witness->demand_tip=$lang['status_unapprove'];
                            else
                                $witness->demand_tip=$lang['status_unapprove_admin'];
                        }
                        else
                        {
                            if (!$show_notapprove)
                                $witness->demand_tip=$lang['status_approved'];
                            else
                                $witness->demand_tip=$lang['status_approved_admin'];
                        }
                        $witness->demand_tip = str_replace("%u",$wapprove_username,$witness->demand_tip);
                        $witness->demand_tip = str_replace("%d",$wapprove_date,$witness->demand_tip);
                        $witness->demand_tip = str_replace("%t",$wapprove_time,$witness->demand_tip);
                    }
                    demandmodule::demand_split_content($witness->content, $witness->content_hidden);
                }
                $value->witness=$temp;
                // end of for witness - end
            }
            else
            {
                $value->witness=null;
            }
        }
    }
}